package device

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/device"
	deviceReq "github.com/flipped-aurora/gin-vue-admin/server/model/device/request"
)

type AllVersionsService struct{}

// CreateAllVersions 创建allVersions表记录
// Author [yourname](https://github.com/yourname)
func (allVersionsService *AllVersionsService) CreateAllVersions(ctx context.Context, allVersions *device.AllVersions) (err error) {
	err = global.GVA_DB.Create(allVersions).Error
	return err
}

// DeleteAllVersions 删除allVersions表记录
// Author [yourname](https://github.com/yourname)
func (allVersionsService *AllVersionsService) DeleteAllVersions(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&device.AllVersions{}, "id = ?", ID).Error
	return err
}

// DeleteAllVersionsByIds 批量删除allVersions表记录
// Author [yourname](https://github.com/yourname)
func (allVersionsService *AllVersionsService) DeleteAllVersionsByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]device.AllVersions{}, "id in ?", IDs).Error
	return err
}

// UpdateAllVersions 更新allVersions表记录
// Author [yourname](https://github.com/yourname)
func (allVersionsService *AllVersionsService) UpdateAllVersions(ctx context.Context, allVersions device.AllVersions) (err error) {
	err = global.GVA_DB.Model(&device.AllVersions{}).Where("id = ?", allVersions.ID).Updates(&allVersions).Error
	return err
}

// GetAllVersions 根据ID获取allVersions表记录
// Author [yourname](https://github.com/yourname)
func (allVersionsService *AllVersionsService) GetAllVersions(ctx context.Context, ID string) (allVersions device.AllVersions, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&allVersions).Error
	return
}

// GetAllVersionsInfoList 分页获取allVersions表记录
// Author [yourname](https://github.com/yourname)
func (allVersionsService *AllVersionsService) GetAllVersionsInfoList(ctx context.Context, info deviceReq.AllVersionsSearch) (list []device.AllVersions, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&device.AllVersions{})
	var allVersionss []device.AllVersions
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}

	if info.EquipmentModel != nil && *info.EquipmentModel != "" {
		db = db.Where("equipment_model = ?", *info.EquipmentModel)
	}
	if info.Version != nil && *info.Version != "" {
		db = db.Where("version = ?", *info.Version)
	}
	if info.ApplicationSoftware != nil && *info.ApplicationSoftware != "" {
		db = db.Where("application_software LIKE ?", "%"+*info.ApplicationSoftware+"%")
	}
	if info.SystemControl != nil && *info.SystemControl != "" {
		db = db.Where("system_control LIKE ?", "%"+*info.SystemControl+"%")
	}
	if info.UpgradeRequirements != nil && *info.UpgradeRequirements != "" {
		db = db.Where("upgrade_requirements = ?", *info.UpgradeRequirements)
	}
	if info.ReleaseNote != nil && *info.ReleaseNote != "" {
		db = db.Where("release_note LIKE ?", "%"+*info.ReleaseNote+"%")
	}
	if info.VersionPackage != nil && *info.VersionPackage != "" {
		db = db.Where("version_package LIKE ?", "%"+*info.VersionPackage+"%")
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&allVersionss).Error
	return allVersionss, total, err
}
func (allVersionsService *AllVersionsService) GetAllVersionsPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
